<?php
/**
 * Created by PhpStorm.
 * User: LENOVO
 * Date: 2017/7/13
 * Time: 16:01
 */
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;

class Login extends Controller
{
    public  function index()
    {
        return $this->fetch();
    }

    //验证消息
    public  function login_data()
    {
        //获取请求数据
        session_start();
        $data=Request::instance()->param();
        $UserName=$data['username'];
        $PassWord=md5($data['password']);
        if($UserName==null||$PassWord==null)
        {
            return json(['code'=>0,'msg'=>'用户和密码不能为空']);
        }
        $captcha=input("post.verify","");
        if(empty($captcha)){
            return json(['code'=>0,'data'=>$data,'msg'=>'验证码不能为空!']);
        }
        if(!captcha_check($captcha)){
            return json(['code'=>-1,'data'=>$data,'msg'=>'验证码错误!']);
        }else{

             //验证码正确的情况下
             $result=Db::query("select * from x_system_user where user_name=:user_name AND password=:password",['user_name'=>$UserName,'password'=>$PassWord]);
             if(!$result)
             {
                 return json(['code'=>0,'msg'=>'用户或密码错误']);
             }
            //登陆成功存session
            $_SESSION['username']=$UserName;
            $_SESSION['user_id'] =$result[0]['user_id'];
            return json(['code'=>1,'url'=>'/admin/index/index','msg'=>'欢迎登陆']);

        }


    }
}